const useSelect = initInfo => {
  // 选中某一个资源|文件夹
  const selectedRow = v => {
    if ((initInfo.value.isChooseResource && !v.isFolder) || !initInfo.value.isChooseResource) {
      initInfo.value.sourceList.forEach(item => {
        if (initInfo.value.isChooseResource && !initInfo.value.isMultiple) {
          item.active = false
        }
        if (item.id === v.id && item.isFolder === v.isFolder) {
          if (initInfo.value.isChooseResource) {
            if (!item.isFolder) {
              item.active = !item.active
            }
          } else {
            item.active = !item.active
          }
          if (item.active) {
            if (item.isFolder) {
              initInfo.value.selectedInfo.folderCount += 1
            } else {
              initInfo.value.selectedInfo.sourceCount += 1
              if (item.position > 0) {
                initInfo.value.selectedInfo.usedCheckedCount += 1
              }
            }
          } else {
            if (item.isFolder) {
              initInfo.value.selectedInfo.folderCount -= 1
            } else {
              initInfo.value.selectedInfo.sourceCount -= 1
              if (item.position > 0) {
                initInfo.value.selectedInfo.usedCheckedCount -= 1
              }
            }
          }
        }
      })
    }
    if (initInfo.value.selectedInfo.folderCount + initInfo.value.selectedInfo.sourceCount === initInfo.value.sourceList.length) {
      initInfo.value.selectedInfo.allChecked = true
      initInfo.value.selectedInfo.isIndeterminate = false
    } else {
      initInfo.value.selectedInfo.allChecked = false
      initInfo.value.selectedInfo.isIndeterminate = true
    }
    if (initInfo.value.selectedInfo.folderCount + initInfo.value.selectedInfo.sourceCount === 0) {
      initInfo.value.selectedInfo.checkedTitle = '全选'
      initInfo.value.selectedInfo.isIndeterminate = false
    } else {
      initInfo.value.selectedInfo.checkedTitle = '已选中' + (initInfo.value.selectedInfo.folderCount + initInfo.value.selectedInfo.sourceCount) + '个文件'
    }
    if (initInfo.value.selectedInfo.usedCheckedCount > 0) {
      initInfo.value.selectedInfo.isUsedChecked = true
    } else {
      initInfo.value.selectedInfo.isUsedChecked = false
    }

    if (initInfo.value.selectedInfo.folderCount === 0 && initInfo.value.selectedInfo.sourceCount === 1) {
      initInfo.value.sourceList.forEach(item => {
        if (item.active) {
          initInfo.value.selectedInfo.activeSingleSource = item
        }
      })
    } else {
      initInfo.value.selectedInfo.activeSingleSource = null
    }
  }

  // 全选|全不选资源、文件夹事件
  const checkedAllHandler = () => {
    initInfo.value.selectedInfo.isIndeterminate = false
    if (initInfo.value.selectedInfo.allChecked) {
      initInfo.value.sourceList.forEach(item => {
        if (!item.active) {
          item.active = true
          if (item.isFolder) {
            initInfo.value.selectedInfo.folderCount += 1
          } else {
            initInfo.value.selectedInfo.sourceCount += 1
            if (item.position > 0) {
              initInfo.value.selectedInfo.usedCheckedCount += 1
            }
          }
        }
      })
      initInfo.value.selectedInfo.checkedTitle = '已选中' + (initInfo.value.selectedInfo.folderCount + initInfo.value.selectedInfo.sourceCount) + '个文件'
      if (initInfo.value.selectedInfo.usedCheckedCount > 0) {
        initInfo.value.selectedInfo.isUsedChecked = true
      } else {
        initInfo.value.selectedInfo.isUsedChecked = false
      }
    } else {
      initInfo.value.sourceList.forEach(item => {
        item.active = false
      })
      initInfo.value.selectedInfo.checkedTitle = '全选'
      initInfo.value.selectedInfo.folderCount = 0
      initInfo.value.selectedInfo.sourceCount = 0
      initInfo.value.selectedInfo.usedCheckedCount = 0
      initInfo.value.selectedInfo.isUsedChecked = false
    }
  }

  // 表格类型布局的单元格点击事件
  const cellClickHandler = (row, column) => {
    if (initInfo.value.isRecycle || initInfo.value.isChooseResource) {
      if (column.property !== 'operate') {
        selectedRow(row)
      }
    } else {
      if (column.property !== 'title' && column.property !== 'operate') {
        selectedRow(row)
      }
    }
  }

  return {
    selectedRow,
    checkedAllHandler,
    cellClickHandler
  }
}
export default useSelect
